package arithmetic.leetCode;

/**
 * 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
 * <p>
 * 找到给定的二维数组中最大的岛屿面积。(如果没有岛屿，则返回面积为0。)
 * <p>
 * 示例 1:
 * <p>
 * [[0,0,1,0,0,0,0,1,0,0,0,0,0],
 * [0,0,0,0,0,0,0,1,1,1,0,0,0],
 * [0,1,1,0,1,0,0,0,0,0,0,0,0],
 * [0,1,0,0,1,1,0,0,1,0,1,0,0],
 * [0,1,0,0,1,1,0,0,1,1,1,0,0],
 * [0,0,0,0,0,0,0,0,0,0,1,0,0],
 * [0,0,0,0,0,0,0,1,1,1,0,0,0],
 * [0,0,0,0,0,0,0,1,1,0,0,0,0]]
 * 对于上面这个给定矩阵应返回 6。注意答案不应该是11，因为岛屿只能包含水平或垂直的四个方向的‘1’。
 * <p>
 * 示例 2:
 * <p>
 * [[0,0,0,0,0,0,0,0]]
 * 对于上面这个给定的矩阵, 返回 0。
 * <p>
 * 注意: 给定的矩阵grid 的长度和宽度都不超过 50。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/max-area-of-island
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 *
 * @author dycong
 * @date 2019/11/14 8:48
 */
public class MaxAreaOfIsland_695 {

    public static void main(String[] args) {
        MaxAreaOfIsland_695 maxAreaOfIsland_695 = new MaxAreaOfIsland_695();
        long t = System.currentTimeMillis();

        int[][] grid = {
                {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
                {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
                {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}
        };

        int[][] grid1 = {
                {1, 1, 0, 1, 1},
                {1, 0, 0, 0, 0},
                {0, 0, 0, 0, 1},
                {1, 1, 0, 1, 1}
        };
        int[][] grid2 = {
                {0, 1},
                {1, 1}
        };

//        System.out.println(maxAreaOfIsland_695.maxAreaOfIsland(grid) + "   " + (System.currentTimeMillis() - t));
//        System.out.println(maxAreaOfIsland_695.maxAreaOfIsland(grid1) + "   " + (System.currentTimeMillis() - t));
        System.out.println(maxAreaOfIsland_695.maxAreaOfIsland(grid2) + "   " + (System.currentTimeMillis() - t));
    }

    public int maxAreaOfIsland(int[][] grid) {

        int row = grid.length;
        int col = grid[0].length;
        int max = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 1) {
                    int cur = dfs(grid, i, j);
                    if (cur > max) {
                        max = cur;
                    }
                }
            }
        }
        return max;
    }


    private int dfs(int[][] grid, int i, int j) {
        if (grid[i][j] != 1) {
            return 0;
        }

        int ret = 1;
        grid[i][j] = 0;
        if (i - 1 >= 0 && grid[i - 1][j] == 1)
            ret += dfs(grid, i - 1, j);

        if (j - 1 >= 0 && grid[i][j - 1] == 1)
            ret += dfs(grid, i, j - 1);

        if (i + 1 < grid.length && grid[i + 1][j] == 1)
            ret += dfs(grid, i + 1, j);

        if (j + 1 < grid[0].length && grid[i][j + 1] == 1)
            ret += dfs(grid, i, j + 1);
        return ret;
    }


    //        Set<Integer> visited = new HashSet<>();

}
